﻿$(function() {
    /**
     * 消耗电镀
     * */
    var electricitybar = echarts.init(document.getElementById('electricityOpt'));
    optionBar1 = {
    		tooltip : {
    	        trigger: 'axis',
    	        axisPointer : {            
    	            type : 'shadow'        
    	        },
    	        position: function (point, params, dom) {
    	            // 固定在顶部
    	            return [point[0], '10%'];
    	        }
    	    },
    	    grid: {
	            top: '25%',
	            left: '4%',
	            right: '8%',
	            bottom:'4%',
	            containLabel: true
	        },
    	    legend: {
    	    	top: '5',
    	    	right:'20',
    	        data:['电度']
    	    },
    	    backgroundColor:'#e0e0e0',
    	    color:['#8dc63f'],
    	    xAxis:  {
            	type: 'category',
    	        data: [],//['OP100','OP200','OP300','OP400','OP500'],
    	        splitLine:{
	            	show:false
	            },
	            axisLabel :{  
				    interval:0
				} 
    	    },
    	    yAxis: {
    	    	 min:0,
    	    	 type: 'value',
    	    	 name:'每日电度(度)',
    	    	 axisLabel:{
    	    		 formatter: '{value}'
    	    	 },
    	    	 splitLine:{
 	            	show:false
 	            }
    	    },
    	    series: [{
    	    	name: '电度',
	            stack: '总量',
	            type: 'bar',
	            barMaxWidth:40,
	            data: []//[10,20,30,40,50]
    	    }]
	};
    electricitybar.setOption(optionBar1);
   
    
    /**
     * 电费
     * */
    var chartbar = echarts.init(document.getElementById('chartOpt'));
    optionBar2 = {
    		tooltip : {
    	        trigger: 'axis',
    	        axisPointer : {            
    	            type : 'shadow'        
    	        },
    	        position: function (point, params, dom) {
    	            // 固定在顶部
    	            return [point[0], '10%'];
    	        }
    	    },
    	    grid: {
	            top: '25%',
	            left: '4%',
	            right: '8%',
	            bottom:'4%',
	            containLabel: true
	        },
    	    legend: {
    	    	 top: '0',
    	    	right:'20',
    	        data:['电费']
    	    },
    	    backgroundColor:'#e0e0e0',
    	    color:['#40bbea'],
    	    xAxis:  {
            	type: 'category',
    	        data: [],//['OP100','OP200','OP300','OP400','OP500'],
    	        splitLine:{
	            	show:false
	            },
	            axisLabel :{  
				    interval:0
				   // rotate:40 
				} 
    	    },
    	    yAxis: {
    	    	 min:0,
    	    	 type: 'value',
    	    	 name:'电费(元)',
    	    	 axisLabel:{
    	    		 formatter: '{value}'
    	    	 },
    	    	 splitLine:{
 	            	show:false
 	            }
    	    },
    	    series: [{
    	    	name: '电费',
	            stack: '总量',
	            type: 'bar',
	            barMaxWidth:40,
	            data: []//[10,20,30,40,50]
    	    }]
	};
    chartbar.setOption(optionBar2);
    
    /**
     * 满载率
     * */
    var ratebar = echarts.init(document.getElementById('rateOpt'));
    optionBar3 = {
    		tooltip : {
    	        trigger: 'axis',
    	        axisPointer : {            
    	            type : 'shadow'        
    	        },
    	        position: function (point, params, dom) {
    	            // 固定在顶部
    	            return [point[0], '10%'];
    	        }
    	    },
    	    grid: {
	            top: '25%',
	            left: '4%',
	            right: '8%',
	            bottom:'4%',
	            containLabel: true
	        },
    	    legend: {
    	    	top: '5',
    	    	right:'20',
    	        data:[]//['满载','空载'],
    	    },
    	    backgroundColor:'#e0e0e0',
    	    color:['#f7aa47','#999933'],
    	    xAxis:  {
            	type: 'category',
    	        data: [],//['OP100','OP200','OP300','OP400','OP500'],
    	        splitLine:{
	            	show:false
	            },
	            axisLabel :{  
				    interval:0
				}
    	    },
    	    yAxis: {
    	    	 min:0,
    	    	 max:100,
    	    	 type: 'value',
    	    	 name:'满载率(%)',
    	    	 axisLabel:{
    	    		 formatter: '{value}'
    	    	 },
    	    	 splitLine:{
 	            	show:false
 	            }
    	    },
    	    series: []
	};
    ratebar.setOption(optionBar3);
    
    /**
     * 初始化
     * */
    
    var vm = new Vue({
	    el: '#itemData',
	    data:{item:[]}
	}); 
    
    $('#cc').combobox({    
        onChange:function(value){
        	var getsliderTime=$('#ss').slider('getValue');
        	time=moment(getsliderTime).format("YYYY-MM-DD")+" "+value+":00:00";
        	getData();
        }
    }); 
    var lineId=0;
    var minute=$("#cc").datebox("getValue");
    $('#ss').slider('setValue',parseInt(moment().format("x")));
    var getsliderTime=$('#ss').slider('getValue');
	var	time=moment(getsliderTime).format("YYYY-MM-DD")+" "+minute+":00:00";
	getData();

    function getData(){
    	$.post(basePath+"/compare/electricitycompare/day.json",{"lineId":lineId,belongTime:time},function(res){
    		vm.$set('item',res);
//    		if(res.success){
			electricitybar.setOption({
		        xAxis: {
		            data: res.equNames
		        },
		        series: [{
		            data: res.electricityBar
		        }]
			});
			chartbar.setOption({
		        xAxis: {
		            data: res.equNames
		        },
		        series: [{
		            data: res.chargeBar
		        }]
			});
			var series=[];
		    for(var i = 0;i<res.fullRateBar.length;i++){
		        series.push({
		            name: res.fullRateBar[i].name,
		            stack: '总量',
		            type: 'bar',
		            barMaxWidth:40,
		            data: res.fullRateBar[i].value
		        });
		    }
		    ratebar.setOption({
				legend: {
		            data: res.stateLeg
		        },
				xAxis: {
		            data: res.equNames
		        },
		        series:series
			});
//    		}	
    	});
    }
    
    /**
     * slider滑动事件
     * */
    $('#ss').slider({
       onComplete:function(value){//滑块值改变时触发
       	time=moment(value).format('YYYY-MM-DD')+" "+minute+":00:00";
       	getData();
       }
    });
    
    /**
     * 自定义时间按钮的点击事件
     * */
	 $("#defineTime").click(function (){
		 var mePanel = $('#defineTimeWin');
	   	 mePanel.dialog('open');
	 });
	 /**
	  * 切换生产线的点击事件
	  * */
	var mePanel = $('#switchEquip');
	$("#linetree").tree({
		onDblClick: function(node) {
			if (node.url) {
//				if(node.shifts){
					lineId=node.id;
					time=moment($('#ss').slider('getValue')).format("YYYY-MM-DD")+" "+minute+":00:00";
		    		getData();
					mePanel.dialog('close');
//				}else{
//					$.messager.alert({showSpeed:100,title:'消息提示',msg:'该生产线无班次，请重新选择',timeout:3000});
//				}
			}else{
				$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择生产线！',timeout:3000});
			}
		}
	});
   $("#switchEquipOK").click(function(){
   	var row = $("#linetree").tree('getSelected');
   	if(row.url){
//   		if(row.shifts){
   			lineId=row.id;
   			time=moment($('#ss').slider('getValue')).format("YYYY-MM-DD")+" "+minute+":00:00";
   			getData();
   			$('#switchEquip').dialog('close');
//   		}else{
//				$.messager.alert({showSpeed:100,title:'消息提示',msg:'该生产线无班次，请重新选择',timeout:3000});
//			}
   	}else{
      		$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择一条生产线!',timeout:3000});
      	}
   });
});